package edu.mtu.citizenscience.mushroommapper.model.family;

import com.openswimsoftware.orm.model.Model;
import com.openswimsoftware.orm.model.annotation.Children;
import com.openswimsoftware.orm.model.annotation.Parent;
import com.openswimsoftware.orm.model.exception.InvalidModelField;
import com.openswimsoftware.orm.model.list.ModelList;
import com.openswimsoftware.orm.model.query.where.WhereOperator;

import android.content.Context;

import edu.mtu.citizenscience.mushroommapper.model.ModelFamily;
import edu.mtu.citizenscience.mushroommapper.model.family.genus.ModelSpecies;

/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Model of a mushroom genus in the app DB belongs to 
 * a family has a name and species
 */
public class ModelGenus extends Model<ModelGenus> {
	// reference var for passing genus model id between activities
	public static final String GENUS_ID = "GENUS_ID";
	
	// mushroom genus name
	private String name;
	
	// mushroom genus parent family
	@Parent
	private ModelFamily family;
	
	// the genus has many child species models
	@Children(modelType = ModelSpecies.class)
	private ModelList<ModelSpecies> species;
	
	/**
	 * Create the genus model
	 * 
	 * @param   context - Context - context for model operations
	 * @return	void
	 */
	public ModelGenus(Context context) 
	{
		super(context, ModelGenus.class);
	}

	/**
	 * Create the genus model
	 * 
	 * @param   context - Context - context for model operations
	 * @param	id - long - the id of the model to load
	 * @return	void
	 */
	public ModelGenus(Context context, long id) 
	{
		super(context, ModelGenus.class, id);
	}
	
	/**
	 * Get the genus name
	 * 
	 * @param   void
	 * @return	String - the genus name
	 */
	public String getName() 
	{
		// make sure the model is loaded
		this.load();
		
		return name;
	}
	
	/**
	 * Set the genus name
	 * 
	 * @param   name - String - the genus name
	 * @return	void
	 */
	public void setName(String name) 
	{
		// mark the model as updated
		this.updated = true;
		
		this.name = name;
	}
	
	/**
	 * Get the genus species models
	 * 
	 * @param   void
	 * @return	ModelList<ModelSpecies> - List of species models
	 */
	public ModelList<ModelSpecies> getSpecies() 
	{
		// make sure the model is loaded
		this.load();
		
		return species;
	}
	
	/**
	 * Set the genus species models
	 * 
	 * @param   species - ModelList<ModelSpecies> - List of species models
	 * @return	void
	 */
	public void setSpecies(ModelList<ModelSpecies> species) 
	{
		// mark the model as updated
		this.updated = true;
		
		this.species = species;
	}
	
	/**
	 * Get the genus parent family
	 * 
	 * @param   void
	 * @return	ModelFamily - Genus parent family model
	 */
	public ModelFamily getFamily()
	{
		// make sure the model is loaded
		this.load();
		
		return this.family;
	}
	
	/**
	 * Set the genus parent family
	 * 
	 * @param   family - ModelFamily - Genus parent family model
	 * @return	void
	 */
	public void setFamily(ModelFamily family)
	{
		// mark the model as updated
		this.updated = true;
		
		this.family = family;
	}
	
	/**
	 * Search for the name in genus models
	 * 
	 * @param   name - String - the genus name to find
	 * @return	ModelGenus - model genus found for the name
	 */
	public ModelGenus find(String name)
	{
		try 
		{
			// return genus model where name equals the passed name
			return this.findQuery().where("name", WhereOperator.EQUAL, name).execute();
		} 
		catch (InvalidModelField e) 
		{
			// happens if model is not valid
			e.printStackTrace();
		}
		
		// if a problem return nothing
		return null;
	}
	
}
